package com.example.configclient.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.configclient.entity.User;
import com.example.configclient.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zcx
 * @Title 用户控制器类
 * @date 2019年05月06日 15:19
 **/
@RestController
@RefreshScope  // 这个注解声明了刷新配置的范围，如果使用config配置类的话，就声明到配置类上即可
public class UserController {

    @Autowired
    private UserService service;


    @RequestMapping(value = "/user",produces = "application/json")
    public List<User> getUser() throws IOException {
        //文件输出位置
        OutputStream outputStream = new FileOutputStream("C:\\Users\\cqt123456789\\Desktop\\a.xlsx");
        ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX);
        //写仅有一个Sheet的Excel文件，此场景较为通用
        Sheet sheet = new Sheet(1,0, User.class);
        //第一个sheet名称
        sheet.setSheetName("第一个sheetname");
        //写数据到Writer上下文中
        //入参1:创建要写入的模型数据
        //入参2:要写入的目标sheet
        writer.write(service.getUserListByReqVo(),sheet);
        //将上下文的最终outputStream写入到指定文件中
        writer.finish();
        //关闭流
        outputStream.close();
        return service.getUserListByReqVo();
    }

    @RequestMapping(value = "/get",produces = "application/json")
    public void getExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        String fileName = new String(("User " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8");
        //添加响应头信息
//        response.setHeader("Content-disposition", "attachment; filename=" +fileName+ ".xls");
//        response.setContentType("application/msexcel;charset=UTF-8");//设置类型
//        response.setHeader("Pragma", "No-cache");//设置头
//        response.setHeader("Cache-Control", "no-cache");//设置头
//        response.setDateHeader("Expires", 0);//设置日期头
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" +fileName+ ".xlsx");
        Sheet sheet = new Sheet(1, 0,User.class);
        sheet.setSheetName("第一个sheet");
        writer.write(service.getUserListByReqVo(),sheet);
        writer.finish();
        out.flush();
    }


    @Value("${spring.datasource.url}")
    private String url;

    @GetMapping("/print")
    public String print(){
        return url;
    }
}
